#include "ghmodel.h"
#include "vector"
#include "QDebug"

GHModel::GHModel(BooferGH *data, QObject *parent) : QAbstractTableModel(parent)
{
    boofer = data;
}

int GHModel::rowCount(const QModelIndex& parent) const {
    if ( parent.isValid() || (boofer->data_x.size() == 0) )
        return 0;
    else
        return boofer->data_x.size();
}

int GHModel::columnCount(const QModelIndex& parent ) const {
    Q_UNUSED(parent);

    return m_header.count();
}

QVariant GHModel::headerData( int section,
                                 Qt::Orientation orientation,
                                 int role) const {
    if (!boofer->data_x.size())
        return QVariant();

    if (orientation == Qt::Horizontal) {
        if (role == Qt::DisplayRole)
            return QVariant(m_header[section]);
    }
    else if (orientation == Qt::Vertical) {
        if (role == Qt::DisplayRole)
            return section+1;
    }

    return QAbstractTableModel::headerData(
        section, orientation, role);
}

Qt::ItemFlags GHModel::flags(const QModelIndex &index) const {
    if (!index.isValid() || !boofer->data_x.size())
        return 0;
    return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}

QVariant GHModel::data( const QModelIndex &index, int role) const
{
    if (!index.isValid() || !boofer->data_x.size())
            return QVariant();

    if (role == Qt::SizeHintRole)
            return QSize(30, 12);

    if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
        if(index.column() == 0)
            return boofer->data_x[index.row()];
        if(index.column() == 1)
            return boofer->data_y[index.row()];
    }

    if (role == Qt::ToolTipRole)
    {
        return "";
    }
    return QVariant();
}

bool GHModel::setData(const QModelIndex &index, const QVariant &value, int role)
{

    if (index.isValid() &&
       (role == Qt::EditRole || role == Qt::DisplayRole)) {
        if(index.column() == 0)
            boofer->data_x[index.row()] = value.toDouble();
        if(index.column() == 1)
            boofer->data_y[index.row()] = value.toDouble();
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

bool GHModel::insertRows( int row, int count, const QModelIndex& parent) {
    Q_UNUSED(parent);
    beginInsertRows(QModelIndex(), row, row+count-1);
    std::vector<double>::iterator xit = boofer->data_x.begin();
    std::vector<double>::iterator yit = boofer->data_y.begin();
    xit += row;
    yit += row;
    for (int i=0; i<count; i++)
    {
        xit = boofer->data_x.insert(xit, 0);
        yit = boofer->data_y.insert(yit, 0);
    }
    endInsertRows();
    return true;
}

bool GHModel::push_back_pair(double x, double y, const QModelIndex &parent)
{
    Q_UNUSED(parent);
    int row = this->rowCount();
    beginInsertRows(QModelIndex(), row, row);
    boofer->data_x.push_back(x);
    boofer->data_y.push_back(y);
    endInsertRows();
    return true;
}

bool GHModel::removeRows( int row, int count, const QModelIndex& parent) {
    Q_UNUSED(parent);
    if (!boofer->data_x.size() || row + count > boofer->data_x.size())
        return false;

    beginRemoveRows(QModelIndex(), row, row+count-1);
    std::vector<double>::iterator xit = boofer->data_x.begin();
    std::vector<double>::iterator yit = boofer->data_y.begin();
    xit += row;
    yit += row;
    for (int i=0; i<count; i++)
    {
        xit = boofer->data_x.erase(xit);
        yit = boofer->data_y.erase(yit);
    }

    endRemoveRows();
    return true;
}

void GHModel::setHeader(const QStringList &header)
{
    m_header = header;
}
